﻿package net.wangit.framework.config;

import java.util.HashMap;
import java.util.Map;

import net.wangit.Path;
import net.wangit.framework.exception.ConfigurationException;
import net.wangit.framework.util.CacheUtil;
import net.wangit.framework.util.InterceptorLoader;
import net.wangit.util.FileUtil;
import net.wangit.view.unit.grid.Wheel;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;


public class DomainsSettings {

	private static final Log log = LogFactory
			.getLog(net.wangit.framework.config.DomainsSettings.class);
	private static final String DOMAIN_FILENAME = "domain.xml";
	public static final String DATASOURCE_FILENAME = "datasource.xml";
	private static Map domains;
	private static Map dss;

	public DomainsSettings() {
	}

	public static void load() {
		domains = new HashMap();
		dss = new HashMap();
		DomainSettings global = new DomainSettings();
		String path = (new StringBuilder(Path.getDomainRootPath())).append(
				DomainsSettings.DOMAIN_FILENAME).toString();
		try {
			if (!FileUtil.exists(path)) {
				log.error((new StringBuilder(String.valueOf(path))).append(
						" not found").toString());
				throw new ConfigurationException((new StringBuilder(path)).append(" not found").toString());
			}
			global = DomainSettingsFactory.getDomainSettings(path, global);
		} catch (Exception e) {
			throw new ConfigurationException(path, e);
		}
		InterceptorLoader.loadDefaultInterceptors();
		String[] list = FileUtil.listDirectorys(Path.getDomainRootPath());
		for (int i = 0; i < list.length; i++) {
			String domain = list[i];
			path = (new StringBuilder(Path.getDomainPath(domain))).append(
					DomainsSettings.DATASOURCE_FILENAME).toString();
			if (FileUtil.exists(path))
				if (GlobalSettings.isAccessibleDomain(domain)) {
					domains.put(domain, null);
					path = (new StringBuilder(Path.getDomainPath(domain)))
							.append(DomainsSettings.DOMAIN_FILENAME).toString();
					dss.put(domain, DomainSettingsFactory.getDomainSettings(
							path, global));
					InterceptorLoader.loadDomainInterceptors(domain);
				} else {
					log.debug((new StringBuilder("clear cached objects: "))
							.append(domain).toString());
					CacheUtil.getActionMappingCache(domain).clearAll();
					CacheUtil.getPageCache(domain).clearAll();
					CacheUtil.getUnitCache(domain).clearAll();
					CacheUtil.getEnumerationCache(domain).clearAll();
					CacheUtil.getEnumerationConfigCache(domain).clearAll();
					CacheUtil.getTreeCache(domain).clearAll();
					CacheUtil.getTreeConfigCache(domain).clearAll();
					CacheUtil.getTableCache(domain).clearAll();
					CacheUtil.getChartCache(domain).clearAll();
					CacheUtil.getDataPrivilegeCache(domain).clearAll();
					CacheUtil.getIncrementorCache(domain).clearAll();
				}
		}

		log.info("load domains settings successfully.");
	}

	private static DomainSettings getSettings(String domain) {
		DomainSettings ps = (DomainSettings) dss.get(domain);
		if (ps == null)
			throw new ConfigurationException((new StringBuilder(
					"domain settings of ")).append(domain).append(" not found")
					.toString());
		else
			return ps;
	}

	public static String getDatasource(String domain) {
		return getSettings(domain).getDatasource();
	}

	public static String getDateFormat(String domain) {
		return getSettings(domain).getDateFormat();
	}

	public static String getDefaultAction(String domain) {
		return getSettings(domain).getDefaultAction();
	}

	public static String getDefaultProxy(String domain) {
		return getSettings(domain).getDefaultProxy();
	}

	public static String getWindowTitlePrefix(String domain) {
		return getSettings(domain).getWindowTitlePrefix();
	}

	public static String getWindowTitleSuffix(String domain) {
		return getSettings(domain).getWindowTitleSuffix();
	}

	public static String getFramesetTemplate(String domain) {
		return getSettings(domain).getFramesetTemplate();
	}

	public static String getPageTemplate(String domain) {
		return getSettings(domain).getPageTemplate();
	}

	public static String getUnitTemplate(String domain) {
		return getSettings(domain).getUnitTemplate();
	}

	public static int getListSize(String domain) {
		return getSettings(domain).getListSize();
	}

	public static int getPagesMax(String domain) {
		return getSettings(domain).getPagesMax();
	}

	public static Wheel getRowsConfig(String domain) {
		return getSettings(domain).getRowsConfig();
	}

	public static String getSkin(String domain) {
		return getSettings(domain).getSkin();
	}

	public static boolean isMarkInvalidElements(String domain) {
		return getSettings(domain).isMarkInvalidElements();
	}

	public static String getMarkStyle(String domain) {
		return getSettings(domain).getMarkStyle();
	}

	public static String getTimeFormat(String domain) {
		return getSettings(domain).getTimeFormat();
	}

	public static String getIndexUrl(String domain) {
		return getSettings(domain).getIndexUrl();
	}

	public static String getTimestampFormat(String domain) {
		return getSettings(domain).getTimestampFormat();
	}

	public static boolean isValidDomain(String domain) {
		return domains.containsKey(domain);
	}

}
